import 'package:flutter/material.dart';

import 'explore_page.dart';
import 'home_page.dart';
import 'message_page.dart';
import 'my_page.dart';
import 'plulish_page.dart';

class MainAppPage extends StatefulWidget {
  final String? tab;

  const MainAppPage({super.key, this.tab});

  @override
  State<MainAppPage> createState() => _MainAppPageState();
}

class _MainAppPageState extends State<MainAppPage> {
  int _selectedIndex = 0;

  final List<Widget> _pages = [
    const HomePage(),
    const ExplorePage(),
    const PublishPage(),
    MessagePage(),
    const MyPage(),
  ];

  @override
  void initState() {
    super.initState();
    _setTabFromParam(widget.tab);
  }

  void _setTabFromParam(String? tab) {
    switch (tab) {
      case 'home':
        _selectedIndex = 0;
        break;
      case 'search':
        _selectedIndex = 1;
        break;
      case 'publish':
        _selectedIndex = 2;
        break;
      case 'message':
        _selectedIndex = 3;
        break;
      case 'profile':
        _selectedIndex = 4;
        break;
      default:
        _selectedIndex = 0;
    }
  }

  void _onItemTapped(int index) {
    setState(() => _selectedIndex = index);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: _pages[_selectedIndex],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _selectedIndex,
        onTap: _onItemTapped,
        type: BottomNavigationBarType.fixed,
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: '首页'),
          BottomNavigationBarItem(icon: Icon(Icons.explore), label: '发现'),
          BottomNavigationBarItem(icon: Icon(Icons.add_box), label: '发布'),
          BottomNavigationBarItem(icon: Icon(Icons.message), label: '消息'),
          BottomNavigationBarItem(icon: Icon(Icons.person), label: '我的'),
        ],
      ),
    );
  }
}
